<template>
  <div id="app">
    <a-input placeholder="请输入任务" class="my_ipt" v-model="inputVal" @change="inputChange"/>
    <a-button type="primary" @click="addItemBtn">添加事项</a-button>

    <a-list bordered :dataSource="showLis" class="dt_list">
      <a-list-item slot="renderItem" slot-scope="item">
        <!-- 复选框 -->
        <a-checkbox @change="(e)=>{cbChange(e,item.id)}" :checked="item.done">{{item.info}}</a-checkbox>
        <!-- 删除链接 -->
        <a slot="actions" @click="delItem(item.id)">删除</a>
      </a-list-item>

      <!-- footer区域 -->
      <div slot="footer" class="footer">
        <!-- 未完成的任务个数 -->
        <span>{{noDoneList.length}}条剩余</span>
        <!-- 操作按钮 -->
        <a-button-group>
          <a-button @click="changeShow('All')" :type="showList==='All'?'primary':'default'">全部</a-button>
          <a-button @click="changeShow('Undone')" :type="showList==='Undone'?'primary':'default'">未完成</a-button>
          <a-button @click="changeShow('Done')" :type="showList==='Done'?'primary':'default'">已完成</a-button>
        </a-button-group>
        <!-- 把已经完成的任务清空 -->
        <a @click="clearDone">清除已完成</a>
      </div>
    </a-list>
  </div>
</template>

<script>
import { mapMutations, mapGetters } from 'vuex'
export default {
  name: 'app',
  data () {
    return {
      inputVal: '',
      showList: 'All'
    }
  },
  methods: {
    ...mapMutations(['addItem', 'infoChange', 'removeItem', 'changeStatus']),
    // 输入框内容变化
    inputChange () {
      this.infoChange(this.inputVal)
    },
    // 添加任务按钮
    addItemBtn () {
      if (this.inputVal.trim().length === 0) {
        return this.$message.warning('内容不能为空')
      }
      this.addItem()
      this.inputVal = ''
    },
    // 删除任务
    delItem (id) {
      this.removeItem(id)
    },
    // 复选框状态变化
    cbChange (e, id) {
      const params = {
        status: e.target.checked,
        id: id
      }
      this.changeStatus(params)
    },
    // 清除已完成
    clearDone () {
      this.$store.commit('clearDone')
    },
    // 改变显示列表
    changeShow (list) {
      this.showList = list
      this.$store.commit('showLis', list)
    }
  },
  // 初始化列表
  created () {
    this.$store.dispatch('getlist')
  },
  computed: {
    ...mapGetters(['noDoneList', 'showLis'])
  }
}
</script>

<style scoped>
  #app {
    padding: 10px;
  }

  .my_ipt {
    width: 500px;
    margin-right: 10px;
  }

  .dt_list {
    width: 500px;
    margin-top: 10px;
  }

  .footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
</style>
